/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","msgServ", "ajaxServ", "formServ", "sysmanMain","bizProto","btnServ",
	"uploadfiles","panelUtil","buttonEvent"],
		function($,datagridServ, msgServ, ajaxServ, formServ,sysmanMain, bizProto,btnServ,
			uploadfiles,panelUtil,buttonEvent) {

			'use strict';

			var _bizProto,_buttonConfig,_moduleConfig,_defaultConfig,
				//锚记datagrid目标元素
				$dg,$typegrid,$detailgrid,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,window_btn,
				$edit_window_height=700,$edit_window_width=900,
				//设置密码弹窗对象及按钮对象
				$password_window,password_btn,
				//业务表单对象
				$form,selectedRecord,
				//部门选择框字段
				$deptNameField, $deptIdField,formEl, formData = 0,
				saveUrl = '/docdetail/save.action',
				typeid,
				docdetailEditUrl='/document/query/moduleEditPage.action'
				

			var public_method = {
				
				
				init:function(doc){
					if(window.console){
						console.log('docedit loading ...')
					}
					private_method.init(doc);
				},
				destroy:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					$edit_window = $('#'+ _moduleConfig.edit_window);
					window_btn = $($edit_window).find('div.edit-btn a.function-btn')
					$(window_btn).unbind('click')
					
					
					var file_btn = $($edit_window).find(' a.file_upload_btn')
					$(file_btn).unbind('click')
					$(_defaultConfig.edit_window).empty()
				},
				/**
				 * 
				 * @param _options.grid	  文档列表
				 * @param _options.flag	  新增或编辑标志
				 * @param _options.window	编辑窗口
				 * @param _options.typeid 分类id
				 */
				pageShow:function(_options){
					var _pageConfig = {
						width:$edit_window_width,
						height:$edit_window_height
					}
					
					$.extend(_pageConfig,_options)
					
					
					var _url
					var _config = sysmanMain.getCurrentModuConfig();
					
					var _code
					if(_config.addition_param){
						_code = $.parseJSON(_config.addition_param)						
					}
					
					if(_code && _code.module){
						_url = '/'+sysmanMain.getRootPath() +docdetailEditUrl +'?module='+_code.module
					}else{
						_url = '/'+sysmanMain.getRootPath() + docdetailEditUrl
					}
					  
					
					var _param = {
						grid:_options.grid,
						window:_options.window,
						href:_url,
						width:_pageConfig.width,
						height:_pageConfig.height,
						flag:_options.flag,
						existCheckElement:'form'					
					}
					if(_options.onWindowClose){
						_param.onClose=_defaultConfig.onWindowClose
					}
					datagridServ.windowEdit(_param);
				}
				
			}

			var private_method = {
				init : function(_options) {
					if (window.console) {
					console.info('docdetailEdit modules gets ready and been loaded.');
					}
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					
					if(!_moduleConfig || !_moduleConfig.editInitialized){
					_defaultConfig ={
						doc:null,
						rootId:null,				
						panelSelector:'div.well',
						subSystemCode:'sysman',
						edit_window:'win-doc-manage',
						moduleCode:'docdetail',
						upload_queue_id:null,
						upload_file_id:null,
						doccode:null,						
						initElement:null, //初始化扩展
						buttonExtends:null,//按钮扩展
						beforeSubmit:null,//提交前执行的数据处理函数
						editInitialized:false
					}
					
					_defaultConfig = $.extend({},_defaultConfig,_moduleConfig,_options)
					if(_defaultConfig.bizcode)
						_defaultConfig.moduleCode = _defaultConfig.bizcode
					//初始化模块配置					
					_bizProto = new bizProto(_defaultConfig);
					_bizProto.gridHeight -= 15	
					_defaultConfig.bizProto = _bizProto								
					
					//按钮设置
					_buttonConfig={
						
						 "保存":private_method.submit,
						 "取消":private_method.cancel	,
						 "上传":private_method.upload,
						 "附件":private_method.uploadInit,
						 "删除":private_method.delupload,
						 "返回":private_method.changePanel,
						 "提交审核":private_method.auditSubmit,
						 "审核通过":private_method.auditPermit,
						 "审核未通过":private_method.auditDenied
					}
					if(_defaultConfig.buttonExtends){
						var _extends
						if($.isFunction(_defaultConfig.buttonExtends)){
						 	_extends = _defaultConfig.buttonExtends()
						}else{
							_extends = _defaultConfig.buttonExtends
						}
						$.extend(_buttonConfig,_extends)
					}
					
					if(!_defaultConfig.edit_window){
						_defaultConfig.edit_window = 'win-doc-manage'						
					}
					$edit_window = $('#'+ _defaultConfig.edit_window);
					_defaultConfig.buttonConfig = _buttonConfig
					_defaultConfig.editInitialized = true;
					sysmanMain.setCurrentModuConfig(_defaultConfig);
					private_method.initElement(_bizProto.pageDoc)
					}else{
						private_method.initElement(_moduleConfig.bizProto.pageDoc)
					}
					
				},				
				
				/**
				 * 获取当前明细列表中选中记录
				 */
				getCurrentDetailRecord:function(){
					var _record
					var _tab = sysmanMain.getCurrentTab();
					var _grid = $(_tab).find("table.dg-detail");
					return $(_grid).datagrid('getSelected');
				},
				
				//初始化元素
				initElement:function(doc){
					var tab = sysmanMain.getCurrentTab();

					$typegrid = $(tab).find("table.dg-doctype");
					$grid = $(tab).find("table.dg-detail");
					// 编辑窗口初始化
					
										
					
					// 编辑窗口初始化
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					
					$form = $($edit_window).find('form')
					
					var editTd = $($edit_window).find('div.edit-btn')
					if(editTd.length<=0){
						editTd = $(tab).find('div.edit-btn')
					}
					var _fullBizcode = _moduleConfig.fullBizcode;
					
					if(editTd.length>0){
						var _param = {
							bizcode:_fullBizcode,
							rootPath:_bizProto.rootPath,
							container:editTd,
							callback:private_method.bindBtnEvent}
						btnServ.generateEditBtn(_param);
					}					
					
					selectedRecord=$grid.datagrid('getSelected');
					if(selectedRecord){
						$form.form('load',selectedRecord);						
					}
					
					
					//扩展初始化方法
					if(_moduleConfig.initElement){
						var initParam = {rootId:_moduleConfig.rootId,edit_window:_moduleConfig.edit_window}
						if(selectedRecord){
							initParam.record = selectedRecord
						}
						_moduleConfig.initElement(initParam)
					}		

				},
				
				/**
				 * 
				 */
				changePanel:function(_options){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					
					var $listPanel = $('#' +_moduleConfig.edit_window +'  div.edit-panel' )
					var $formPanel = $('#' +_moduleConfig.edit_window +' div.upload-panel' )
					
					panelUtil.togglePanel({target:$listPanel})
					panelUtil.togglePanel({target:$formPanel})
				},
				uploadInit:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					
					/**
					 * 根据审核状态判断是否显示按钮栏
					 */
					var showToolBar = true
					if(_moduleConfig.auditEnable){
						if(buttonEvent.checkAuditStatus({editWindow:_moduleConfig.edit_window})){
							showToolBar = false
						}						
					}
					$edit_window = $('#'+ _moduleConfig.edit_window);
					var isSaved = false,_id
					var _idEl = $('#' + _moduleConfig.edit_window + ' form.doc-form #id')
					if(_idEl.length==1){
						_id = $(_idEl).val()
						if(_id){
							isSaved = true
						}
					}
					
					if(!isSaved){
						msgServ.Msgslide('保存后才能上传附件');
						return
					}
					private_method.changePanel();
					var _uploadList = $('#' + _moduleConfig.edit_window + ' div.upload-panel div.table-archive')
					
					var _moduleCode
					if(_moduleConfig.typeRow){
						_moduleCode = _moduleConfig.typeRow.bizcode
					}else{
						_moduleCode = 	_moduleConfig.moduleCode
					}
					if(_uploadList.length>0){
						var selectedRecord = private_method.getCurrentDetailRecord()
						var uploadParam = {
							data:{
								bizno:_id,
								bizcode:_moduleCode
							},
							rootPath:_bizProto.rootPath,
							biztableid:_moduleConfig.edit_window + ' div.upload-panel div.table-archive',
							toolbarid : _moduleConfig.edit_window +' div.upload-panel div.archive-btn a.file_upload_btn'
						}						
						
						uploadfiles.initList(uploadParam)
						
						if(_moduleConfig.moduleCode && _id ){
						
							uploadfiles.init({
								data:{
									bizno:_id,
									bizcode:_moduleCode
								},
								queue_id:_moduleConfig.edit_window + ' div.upload-panel form.upload-form #queue',
								file_id:_moduleConfig.edit_window + ' div.upload-panel form.upload-form #file_upload',
								rootPath:_moduleConfig.bizProto.rootPath,
								rootid:_moduleConfig.edit_window})
						}
						
						
					}
				},
				upload:function(){
					uploadfiles.upload()
					
				},				
				
				//绑定按钮事件
				bindBtnEvent:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					$edit_window = $('#'+ _moduleConfig.edit_window);
					
					window_btn = $($edit_window).find('div.edit-btn a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
					var file_btn = $($edit_window).find(' a.file_upload_btn')
					$(file_btn).unbind('click')
					$(file_btn).bind('click',	private_method.function_btn_listenner)
					
				},	
				
				//功能按钮监听函数
				function_btn_listenner:function(e){	
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					
					btnServ.applyButton({buttonEvt:e,buttonConfig:_moduleConfig.buttonConfig})					
				},
				
				// 提交form
				submit : function(evt) {
					var tab = sysmanMain.getCurrentTab();
					
					_moduleConfig = sysmanMain.getCurrentModuConfig();					
					$edit_window = $('#'+_moduleConfig.edit_window);
					
					if(_moduleConfig.auditEnable){
						if(buttonEvent.checkAuditStatus({editWindow:_moduleConfig.edit_window})){
							msgServ.Msgslide('已审核记录无法修改记录内容');
							if(evt && evt.currentTarget){
								$(evt.currentTarget).linkbutton('enable')
							}
							return
						}						
					}
					
					var _typeid  = _moduleConfig.typeid
					
					
					$form = $($edit_window).find('form')
					var _typeElement = $($form).find("#typeId")
					$(_typeElement).val(_typeid)
					
					var externalVal
					if(_moduleConfig.beforeSubmit){
						externalVal = _moduleConfig.beforeSubmit()						
					}
					datagridServ.save({
						form:$form,
						grid:$grid,
						url:_bizProto.rootPath + saveUrl,
						loadParam:{condition:{typeId:_typeid}},
						externalValue:externalVal
					},evt)					
				},				
				
				//
				cancel : function(evt) {
					
					if(evt && evt.currentTarget){
						$(evt.currentTarget).linkbutton('enable')
					}
					$($edit_window).window('close');
					
				},
				delupload:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					if(_moduleConfig.edit_window)
						uploadfiles.del({
							biztableid:_moduleConfig.edit_window + ' div.table-archive',
							rootPath:_moduleConfig.bizProto.rootPath})
				},
				
				/**
				 * 获取审核参数
				 */
				getAuditParam:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					var tab = sysmanMain.getCurrentTab();					
					$grid = $(tab).find("table.dg-detail");
					var _param = {
						editWindow:_moduleConfig.edit_window,
						moduleConfig:_moduleConfig,
						grid:$grid
					}
					return _param
				},
				
				/**
				 * 提交审核
				 */
				auditSubmit:function(){
					
					buttonEvent.auditSubmit(private_method.getAuditParam())
				},
				/**
				 * 审核通过
				 */
				auditPermit:function(){
					
					buttonEvent.auditPermit(private_method.getAuditParam())
				},
				/**
				 * 审核未通过
				 */
				auditDenied:function(){
					
					buttonEvent.auditDenied(private_method.getAuditParam())
				}

			};

			
			return public_method;

		});